//
//  COQAListViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/21.
//

import UIKit
import MJRefresh
import JXSegmentedView

class COQAListViewController: COBaseViewController {
    private var contents: [COHomeFoodListModel] = []
    var qaId = 0
    private var page = 1
    
    private let isMy: Bool
    private let heId: Int
    
    init(_ isMy: Bool = false, heId: Int = 0) {
        self.isMy = isMy
        self.heId = heId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.mj_header?.beginRefreshing()
    }
    
    override func configUI() {
        view.backgroundColor = .rgba(17, 7, 41, 1)
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(navHeight + statusBarHeight)
        }
    }

    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.backgroundColor = .rgba(17, 7, 41, 1)
        view.register(cellWithClass: COQATableViewCell.self)
        view.delegate = self
        view.dataSource = self
        view.separatorStyle = .none
        
        
        view.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.page = 1
            self?.getData()
        })
        
        view.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {[weak self] in
            self?.page += 1
            self?.getData()
        })
        return view
    }()

}

extension COQAListViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: COQATableViewCell.self)
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = COQADetailViewController()
        vc.model = contents[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    private func getData() {
        if isMy {
            getMyData()
            return
        }
        if heId > 0 {
            getHeData()
            return
        }
        CONetwork.sendRequest(provider: homeProvider, target: .qa(page: page, type: qaId)) { text in
            guard let array = text.array else {
                return
            }
            
            let arr = array.map({ dic in
                let model = COHomeFoodListModel.deserialize(from: dic)!

                return model
            })
            if self.page == 1 {
                self.contents = arr
                self.tableView.mj_header?.endRefreshing()
            } else {
                self.contents.append(contentsOf: arr)
            }
            
            if arr.count < 20 {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self.tableView.mj_footer?.endRefreshing()
            }
            
            self.tableView.reloadData()
        } failedBlock: { _, text in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.makeToast(text, position: .center)
        }

    }
    
    private func getMyData() {
        CONetwork.sendRequest(provider: mineProvider, target: .qaList(page: page)) { text in
            guard let array = text.array else {
                return
            }
            
            let arr = array.map({ dic in
                let model = COHomeFoodListModel.deserialize(from: dic)!

                return model
            })
            if self.page == 1 {
                self.contents = arr
                self.tableView.mj_header?.endRefreshing()
            } else {
                self.contents.append(contentsOf: arr)
            }
            
            if arr.count < 20 {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self.tableView.mj_footer?.endRefreshing()
            }
            
            self.tableView.reloadData()
        } failedBlock: { _, text in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.makeToast(text, position: .center)
        }

    }
    
    private func getHeData() {
        CONetwork.sendRequest(provider: heProvider, target: .qa(id: heId, page: page)) { text in
            guard let array = text.array else {
                return
            }
            
            let arr = array.map({ dic in
                let model = COHomeFoodListModel.deserialize(from: dic)!

                return model
            })
            if self.page == 1 {
                self.contents = arr
                self.tableView.mj_header?.endRefreshing()
            } else {
                self.contents.append(contentsOf: arr)
            }
            
            if arr.count < 20 {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self.tableView.mj_footer?.endRefreshing()
            }
            
            self.tableView.reloadData()
        } failedBlock: { _, text in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.makeToast(text, position: .center)
        }

    }
}

extension COQAListViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return self.view
    }
}
